package com.tyqx.hashchainbackend.service.impl;



import com.tyqx.hashchainbackend.service.SysResourceService;
import com.tyqx.hashchainbean.module.SysResource;
import com.tyqx.hashchainbean.vo.PerOrResourceResultVO;
import com.tyqx.hashchainbean.vo.SysResourceVO;
import com.tyqx.hashchaindao.mapper.SysResourceMapper;
import hashchainutil.common.Constants;
import hashchainutil.common.EmptyUtils;
import hashchainutil.common.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service("sysResourceService")
public class SysResourceServiceImpl implements SysResourceService {

    @Resource
    private SysResourceMapper sysResourceMapper;

    public SysResource getById(Long resId) throws Exception {
        return sysResourceMapper.getById(resId);
    }

    @Override
    public List<PerOrResourceResultVO> queryAllResource() throws Exception {
        return sysResourceMapper.getAllResource();
    }

    public Integer getCountByMap(Map<String, Object> param) throws Exception {
        return sysResourceMapper.getCountByMap(param);
    }

    public Integer save(SysResource sysResource) throws Exception {
        return sysResourceMapper.save(sysResource);
    }

    public Integer modify(SysResource sysResource) throws Exception {
        return sysResourceMapper.modify(sysResource);
    }

    public Integer removeById(Long resId) throws Exception {
        return sysResourceMapper.removeById(resId);
    }

    public Page<List<SysResource>> queryPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) throws Exception {
        Integer total = sysResourceMapper.getCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<SysResource> sysResourceList = sysResourceMapper.getListByMap(param);
        page.setRows(sysResourceList);
        return page;
    }

    @Override
    public List<SysResource> queryTree(Map<String, Object> param) {
        return sysResourceMapper.queryTree(param);
    }

    /**
     * 根据父级ID查找下面的所有子级ID
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Long> queryResId(Long parentId) {
        return sysResourceMapper.getResIdByParentId(parentId);
    }

    @Override
    public List<SysResourceVO> getAllByRoleId(Long roleId) {
        return sysResourceMapper.getAllByRoleId(roleId);
    }

}
